<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IderController extends Controller
{
    public function getIndex()
    {
        //查询能在前台显示的帖子
        $data = DB::table('ider')->where('show',2)->get();
        // 解析模板
        return view('Home.ider.ider',['ider'=>$data,]);
    }

    //添加
    public function postAdd(Request $request)
    {
        //判断是否是登录用户
        if(session('name')){

            $data = $request->only(['title','content']);
            $data['time'] = date('Y-m-d H:i:s',time());
            $data['uname'] = session('name');
            $data['status'] = 1;//未读
            $data['show'] = 1;//不显示
        }else{
            $data = $request->only(['title','content']);
            $data['time'] = date('Y-m-d H:i:s',time());
            $data['uname'] = '游客';
            $data['status'] = 1;//未读
            $data['show'] = 1;//不显示
        }

        //插入数据库
        $res = DB::table('ider')->insert($data);
        if($res){
            return redirect('/ider')->with('success','意见已发送,管理员会尽快解决');
        }else{
            return back()->with('error','发表失败');
        }
    }

    //回复
    public function getHuifu($id)
    {
        //提问内容
        $data = DB::table('ider')->where('id',$id)->first();
        //回复内容
        $res = DB::table('huifu')->where('iid',$id)->get();

        //回复条数
        $num = count($res);
        //解析模板
        return view('home.ider.huifu',['ider'=>$data,'huifu'=>$res,'num'=>$num]);
    }

    //回复内容
    public function postAddhuifu(Request $request)
    {
        $data = $request->only(['content','iid']);
        $data['uname'] = session('name');
        $data['time'] = date('Y-m-d H:i:s',time());

        //添加到数据库
        $res = DB::table('huifu')->insert($data);
        //判断
        if($res){
            return back()->with('success','回复成功');
        }else{
            return back()->with('error','回复失败');
        }
    }

}
